﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace EyFramework.SocketServer.Utils
{
    public class NetUitl
    {
        /// <summary>
        /// telnet port
        /// </summary>
        /// <param name="_ip"></param>
        /// <param name="_port"></param>
        /// <returns></returns>
        public static bool CheckPortEnable(string _ip, int _port)
        {
            //将IP和端口替换成为你要检测的
            string ipAddress = _ip;
            int portNum = _port;
            IPAddress ip = IPAddress.Parse(ipAddress);
            IPEndPoint point = new IPEndPoint(ip, portNum);

            bool _portEnable = false;
            try
            {
                using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    sock.Connect(point);
                    //Console.WriteLine("连接{0}成功!", point);
                    sock.Close();

                    _portEnable = true;
                }
            }
            catch (SocketException e)
            {
                //Console.WriteLine("连接{0}失败", point);
                _portEnable = false;
            }
            return _portEnable;
        }
    }
}